package org.example.spring_boot_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.spring_boot_blog.common.pojo.dataobject.BlogInfo;
import org.example.spring_boot_blog.common.pojo.request.AddBlogParam;
import org.example.spring_boot_blog.common.pojo.request.UpBlogParam;
import org.example.spring_boot_blog.common.pojo.response.BlogInfoResponse;
import org.example.spring_boot_blog.common.utils.BeanConver;
import org.example.spring_boot_blog.mapper.BlogInfoMapper;
import org.example.spring_boot_blog.service.BlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
       LambdaQueryWrapper<BlogInfo> queryWrapper=new LambdaQueryWrapper<BlogInfo>()
               .eq(BlogInfo::getDeleteFlag,0).orderByDesc(BlogInfo::getId);
        List<BlogInfo> blogInfos = blogMapper.selectList(queryWrapper);
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo ->
             BeanConver.trans(blogInfo)).collect(Collectors.toList());
//        blogInfoResponses.forEach(i-> System.out.println(i));
        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);
        return BeanConver.trans(blogInfo);
    }

    @Override
    public Boolean addBlog(AddBlogParam param) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(param,blogInfo);
        try{
            int insert = blogMapper.insert(blogInfo);
            if(insert==1){
                return true;
            }
        }catch (Exception e){
            log.error("添加博客失败,e:{}",e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam param) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(param,blogInfo);
        return update(blogInfo);
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return update(blogInfo);
    }

    public BlogInfo selectBlogById(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
        return blogInfo;
    }
    public Boolean update(BlogInfo blogInfo){
        try{
            Integer result = blogMapper.updateById(blogInfo);
            if(result==1){
                return true;
            }
        }catch (Exception e){
            log.error("修改博客失败,e:{}",e);
        }
        return false;
    }
}
